package com.clas.liu.myapplication.view.fragment.impl;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.clas.liu.myapplication.presenter.i.IBaseFragmentPresenter;
import com.clas.liu.myapplication.presenter.impl.BaseFragmentPresenter;

/**
 * fragment的基类
 * Created by liu on 2019/2/28.
 */

public abstract class BaseFragment <P extends BaseFragmentPresenter &IBaseFragmentPresenter> extends Fragment {

    protected P mPresenter;
    private Handler mHandler = new Handler();
    private Toast mToast;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mPresenter = getPresenter();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected abstract P getPresenter();


    public void showToast(@NonNull final String message) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mToast !=null){
                    mToast.cancel();
                }

                if (getActivity() != null) {
                    mToast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
                    mToast.show();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        if(mPresenter!=null){
            mPresenter.onDestroy();
            mPresenter = null;
        }
        super.onDestroyView();
    }
}
